﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FairyGUI;
using UnityEngine;
using DG.Tweening;

namespace Model
{
    [GameUIView(UIViewType.Battle)]
    public class BattleView : GameUIView
    {
        GLoader BigCard;
        BattleCard chooseCard;
        GComponent chooseCardComponent;
        public override string PackageName { get; set; } = "Battle";

        public override string ViewName { get; set; } = "Main";
        //GList HandList;
        public override void Init()
        {
            BigCard = MainView.GetChild("n10").asLoader;
            MainView.GetChild("n11").onClick.Add(() => { FightModel.Single.NextStatus(FightModel.Single.nowPlayer); });
            MainView.GetChild("n9").onClick.Add(() => UseCard(chooseCard));
            TestBattle();
        }

        /*public void SetShowCard(BattleCard battleCard)
        {
            if (battleCard == null)
            {
                //BigCard.icon
            }
        }*/

        public void SetChooseCard(BattleCard card, GComponent component)
        {
            if (chooseCardComponent != null)
            {
                chooseCardComponent.asButton.selected = false;
            }
            component.asButton.selected = true;
            BigCard.icon = component.icon;
            if (card == null)
            {
                BigCard.icon = "";
                chooseCard = null;
                chooseCardComponent = null;
                return;
            }
            BattleCard battleCard = card;
            chooseCard = battleCard;
            Log.Debug($"选中卡牌{chooseCard.CardName}");

            if (chooseCardComponent != null && chooseCardComponent != component) chooseCardComponent.asButton.selected = false;
            chooseCardComponent = component;
            bool bo = (chooseCard.IfUse(FightModel.Single, chooseCard.Owner).Count != 0);
            //Log.Debug(fightModel.nowPlayer.Name);
            //Log.Debug(bo.ToString());
            MainView.GetChild("n9").asButton.enabled = bo;
            MainView.GetChild("n13").asButton.enabled = bo;
            //BigCard.icon = BattleCard.SetCardToUI(BigCard, card);
        }

        public void SetChooseCard(GComponent component)
        {
            if (component == null)
            {
                BigCard.icon = "";
                chooseCard = null;
                chooseCardComponent = null;
                return;
            }
            if (component.data == null) return;            
            BigCard.icon = component.icon;
            BattleCard battleCard = (BattleCard)component.data;
            chooseCard = battleCard;
            Log.Debug($"选中卡牌{chooseCard.CardName}");

            if (chooseCardComponent != null && chooseCardComponent != component) chooseCardComponent.asButton.selected = false;
            chooseCardComponent = component;
            bool bo = (chooseCard.IfUse(FightModel.Single, chooseCard.Owner).Count != 0);
            //Log.Debug(fightModel.nowPlayer.Name);
            //Log.Debug(bo.ToString());
            MainView.GetChild("n9").asButton.enabled = bo;
            MainView.GetChild("n13").asButton.enabled = bo;
        }
        public void UseCard(BattleCard chooseCard)
        {
            if (chooseCard == null) return;
            Log.Debug($"使用了一张牌{chooseCard.CardName}");
            chooseCard.Use();
            chooseCard = null;
        }


        public async void TestBattle()
        {
            string[] CardInfo = new string[] { "C001", "C001-ALPHA", "RM-001", "RM-002","RM-003" };
            FightModel fightModel = new FightModel();
            fightModel.AddComponent<FightCalComponent>();
            //fightModel.AddComponent<TurnSwitchComponent>();
            fightModel.AddComponent<BattleBaseEvent>();
            BattlePlayer battlePlayer = new BattlePlayer() { Name = "player" };
            //battlePlayer.Hp = 0;
            for (int i = 0; i < 10; i++)
            {
                BattleCard card = BattleCardConfig.Single.GetCard(CardInfo[i % CardInfo.Length]);
                card.Owner = battlePlayer;
                battlePlayer[CardPlace.Deck].Cards.Add(card);
            }
            //battlePlayer[CardPlace.Deck].Shuffle();
            battlePlayer.DrawCards(fightModel, 4);
            BattlePlayer battlePlayer2 = new BattlePlayer() { Name = "oppo" };
            for (int i = 0; i < 10; i++)
            {
                BattleCard battleCard = BattleCardConfig.Single.GetCard(CardInfo[i % CardInfo.Length]);
                battleCard.Owner = battlePlayer2;
                battlePlayer2[CardPlace.Deck].Cards.Add(battleCard);
            }
            battlePlayer2.DrawCards(fightModel, 4);


            fightModel.player1 = battlePlayer;
            fightModel.player2 = battlePlayer2;
            fightModel.nowPlayer = fightModel.player1;
            BattleView battleView = (BattleView)Game.Scene.GetComponent<FGuiComponent>().GetView(UIViewType.Battle);
            fightModel.BattleStatus = BattleStatus.Pick;
            
            //battleView.FreshBattle(fightModel);
            FightModel.Single = fightModel;

            battlePlayer.AddComponent<PlayerUIComponent,GComponent,GComponent>(MainView.GetChild("player").asCom,MainView);
            battlePlayer2.AddComponent<PlayerUIComponent, GComponent, GComponent>(MainView.GetChild("opponent").asCom, MainView);

            
            fightModel.AddComponent<WindowManagerComponent, PlayerUIComponent>(battlePlayer.GetComponent<PlayerUIComponent>());
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FreshUI, fightModel);
            await battlePlayer.ChooseCardFromDeck();
            await battlePlayer2.ChooseCardFromDeck();
        }

        public override void OnEnter()
        {
            //throw new NotImplementedException();
        }
       
    }
}
